﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Data.Query;
using WyUnity.Data.TableMapper;

namespace RFID_Interface.DTO
{
    public class DeliveryListResponse
    {
        public char Result { get; set; }
        public string ErrMsg { get; set; }

        public List<DeliveryBillInfo> BillList { get; set; }

        public static DeliveryListResponse Get(string stocknumber, DateTime datebegin, DateTime dateend, WhereClip condition = null)
        {
            try
            {
                List<DeliveryBillInfo> list;

                using (var db = DB.Open())
                {
                    var where = DeliveryBillInfo.pBillType == 1001 & DeliveryBillInfo.pBillerID==0;
                    if (condition != null) where &= condition;
                    if (!string.IsNullOrEmpty(stocknumber))
                    {
                        where &= (DeliveryBillInfo.pSendStockNum == stocknumber | DeliveryBillInfo.pRecieveStockNum == stocknumber);
                    }
                    if (datebegin.Year >= 2000)
                    {
                        where &= DeliveryBillInfo.pDate >= datebegin;
                    }
                    if (dateend.Year <= 2050)
                    {
                        where &= DeliveryBillInfo.pDate <= dateend;
                    }

                    list = db.QueryList<DeliveryBillInfo>(where, null);
                }

                return new DeliveryListResponse { Result = 'S', BillList = list };
            }
            catch (Exception ex)
            {
                return new DeliveryListResponse { Result = 'F', ErrMsg = "系统错误.\n" + ex.Message };
            }
        }
    }

    public class DeliveryBillInfo : ViewBase<DeliveryBillInfo>
    {
        [Map]
        public string BillNo { get; set; }
        [Map]
        public string Remark { get; set; }
        [Map]
        public DateTime Date { get; set; }
        [Map]
        public string Biller { get; set; }
        [Map]
        public int BillerID { get; set; }
        [Map]
        public int Status { get; set; }
        [Map]
        public string SendStockNum { get; set; }
        [Map]
        public string RecieveStockNum { get; set; }
        [Map]
        public int BillType { get; set; }
        [Map]
        public string CustomerNumber { get; set; }
        [Map]
        public string CustomerName { get; set; }
        [Map]
        public string OrderBillNo { get; set; }
        [Map]
        public string Receiver { get; set; }
        [Map]
        public string Address { get; set; }
        [Map]
        public string ReceiverTel { get; set; }
        [Map]
        public int ROB { get; set; }


        public static FieldBase pSendStockNum = new Field<DeliveryBillInfo>(p => p.SendStockNum);
        public static FieldBase pRecieveStockNum = new Field<DeliveryBillInfo>(p => p.RecieveStockNum);
        public static FieldBase pDate = new Field<DeliveryBillInfo>(p => p.Date);
        public static FieldBase pBillType = new Field<DeliveryBillInfo>(p => p.BillType);
        public static FieldBase pBillerID = new Field<DeliveryBillInfo>(p => p.BillerID);
        public static FieldBase pROB = new Field<DeliveryBillInfo>(p => p.ROB);

        public override string GetSQLClip()
        {
            return @"
SELECT  isb.FBillNo BillNo ,
        isb.FTranType BillType ,
        isb.FNote Remark ,
        isb.FDate [Date] ,
        tu.FName Biller ,isnull(isb.FBillerID,0) BillerID,
        isb.FStatus [Status] ,
        case when ts2.FNumber is null then ts.FNumber ELSE ts2.FNumber END SendStockNum ,
        case when ts2.FNumber is null then null ELSE ts.FNumber END  RecieveStockNum,
        [to].FNumber CustomerNumber,
        [to].FName CustomerName,
        so.FBillNo OrderBillNo,
        so.FHeadSelfS0133 Receiver,
        so.FHeadSelfS0141 [Address],
        so.FHeadSelfS0140 ReceiverTel,
        isb.FROB ROB
FROM    ICStockBill_1001 AS isb
        LEFT JOIN t_Stock AS ts ON isb.FDCStockID = ts.FItemID
        LEFT JOIN t_Stock AS ts2 ON isb.FSCStockID = ts2.FItemID
        LEFT JOIN t_User AS tu ON isb.FBillerID = tu.FUserID
        LEFT JOIN SEOrder AS so ON isb.FOrderInterID=so.FInterID
        LEFT JOIN t_Organization AS [to] ON isb.FSupplyID=[to].FItemID
";


        }
    }
}